<template>
  <div>
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :search.sync="query"
        :data="data"
        :page.sync="page"
        :before-open="beforeOpen"
        v-model="form"
        ref="crud"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        :key="timer"
      >
        <template slot-scope="scope" slot="menu">
          <el-button type="text" v-if="permission.fqcCheckList_view" icon="el-icon-view" size="small" @click.stop="handleView(scope.row)">查看包装 </el-button>
        </template>
        <template slot="menuRight">
          <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
        </template>
      </avue-crud>
      <el-dialog ref="dialog" title="查看详情" append-to-body :visible.sync="showDialog" v-if="showDialog" :close-on-click-modal="dialogClose">
        <!-- <avue-form v-model="detailData" :option="detailOption"></avue-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="small" @click="showDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
        </span> -->
        <avue-crud :option="dialogOption" :table-loading="dialogLoading" :data="dialogData" ref="crud2" :key="timer"> </avue-crud>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import { getPage, getDetail } from '@/api/quality/pending-inspection-list.js';

import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';

export default {
  inject: ['reload'],
  components: {
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'PENDING_INSPECTION_LIST',
      form: {
        deptId: '',
        lineId: ''
      },
      search: {},
      query: {},
      loading: true,
      dialogData: [],
      dialogLoading: false,
      isLock: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      dialogOption: {
        height: 'calc(100vh - 485px)',
        calcHeight: 30,
        index: false,
        menu: false,
        border: true,
        addBtn: false,
        refreshBtn: false,
        column: [
          {
            label: '包装条码',
            prop: 'sequenceNumber'
          },
          {
            label: '包装数量',
            prop: 'quantity'
          },
          {
            label: '物料批次',
            prop: 'lotNo'
          },
          {
            label: '供应商编码',
            prop: 'code'
          },
          {
            label: '供应商名称',
            prop: 'partnerName'
          },
          {
            label: '库存中心',
            prop: 'inventoryName'
          },
          {
            label: '储位编码',
            prop: 'areaCode'
          },
          {
            label: '储位名称',
            prop: 'areaName'
          }
        ]
      },
      option: {
        height: 'calc(100vh - 485px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 24,
        searchMenuPosition: 'right',
        border: true,
        index: true,
        indexLabel: '序号',
        selection: true,
        dialogClickModal: false,
        labelWidth: 120,
        searchSpan: 6,
        addBtn: false,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        // 查看
        viewBtn: false,
        menuWidth: 100,
        columnBtn: false,
        column: [
          {
            label: '检验单号',
            prop: 'documentNo',
            search: true,
            minWidth: 150,
            searchOrder: 1
          },
          {
            label: '物料编码',
            prop: 'partNo',
            minWidth: 100,
            search: true,
            searchOrder: 2
          },
          {
            label: '物料名称',
            minWidth: 100,
            search: true,
            prop: 'partName',
            searchOrder: 3
          },
          {
            label: '物料批次',
            minWidth: 100,
            prop: 'lotNo'
          },
          {
            label: '物料规格',
            minWidth: 100,
            prop: 'partModel'
          },
          {
            label: '送检数量',
            prop: 'sendExamineQuantity'
          },
          {
            label: '检验方式',
            prop: 'examineMode',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=inspection_mode',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '送检日期',
            minWidth: 150,
            prop: 'sendExamineDate',
            type: 'datetime',
            search: true,
            searchOrder: 4,
            searchType: 'daterange',
            format: 'yyyy-MM-dd',
            valueFormat: 'yyyy-MM-dd',
            startPlaceholder: '开始日期',
            endPlaceholder: '结束日期'
          }
        ]
      },
      data: [],
      dialogVisible: false,
      dialogForm: {
        isQualified: null
      },
      // 车间列表
      workshopList: [],
      repairSuccessVisible: false,
      repairSuccessForm: {
        scrapReason: ''
      },
      rulesSuccessForm: {
        scrapReason: [{ required: true, message: '请输入报废原因', trigger: 'blur' }]
      },
      // 判断是在那个routeName下的
      selectRouteObj: {
        documentType: 'ODQC'
      },
      timer: new Date().getTime(),
      showDialog: false,
      detailData: {},
      detailOption: {
        submitBtn: false,
        emptyBtn: false,
        labelWidth: '130',
        column: [
          {
            label: '检验条码',
            prop: 'objectSn',
            type: 'textarea',
            rows: 5,
            span: 24,
            disabled: true,
            placeholder: '检验条码'
          },
          {
            label: '不良条码',
            prop: 'NGSn',
            type: 'textarea',
            rows: 5,
            span: 24,
            disabled: true,
            placeholder: '不良条码'
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(['permission']),
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids;
    }
  },
  watch: {},
  mounted() {
    if (this.isLock) {
      this.onLoad(this.page);
      this.$nextTick(() => {
        this.$structuralSplit();
      });
    }
  },
  methods: {
    init() {},
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.sendExamineDate && params.sendExamineDate.length > 0) {
        params.startSendExamineDate = params.sendExamineDate[0];
        params.endSendExamineDate = params.sendExamineDate[1];
        delete params.sendExamineDate;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      this.query.documentType = this.selectRouteObj.documentType;
      //   this.query.descs = 'send_examine_date';
      this.query.statusStr = 1;
      getPage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    recheck() {
      // let ids = [];
      // this.selectionList.forEach((ele) => {
      //   if (ele.status === 2) {
      //     ids.push(ele.id);
      //   }
      // });
      // if (ids.length < 1) {
      //   this.$message({
      //     type: 'warning',
      //     message: '请确保选择的数据是已检验的！'
      //   });
      //   return;
      // }
      // this.$confirm('确定将选择数据重检?', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     return repeatExam(ids);
      //   })
      //   .then(() => {
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: 'success',
      //       message: '操作成功!'
      //     });
      //     this.$refs.crud.toggleSelection();
      //   });
    },
    newKey() {
      this.timer = new Date().getTime();
    },
    handleView(row) {
      this.dialogLoading = true;
      getDetail(row.id).then((res) => {
        this.form = res.data.data;
        console.log(this.form.packingList, 33);
        this.dialogData = this.form.packingList || [];
        this.dialogLoading = false;
      });
      this.showDialog = true;
    }
  }
};
</script>

<style></style>
